/**
 * @author Miguel Diaz//257835
 */
package dao;

import dao.exceptions.NonexistentEntityException;
import dao.exceptions.PreexistingEntityException;
import entity.ComEstablishment;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;

/**
 * this class handle de CRUD operations in mongo db
 * for commertial establishments
 */

public class ComEstablishmentMongoCtrl {

    private DBCollection dbcollection = null;

    public ComEstablishmentMongoCtrl(DBCollection dbcollection) {
        this.dbcollection = dbcollection;
    }

    public DBCollection getDBCollection() {
        return dbcollection;
    }

    public void create(ComEstablishment comEstablishment) throws PreexistingEntityException{
        DBCollection dbc = getDBCollection();
        BasicDBObject document = new BasicDBObject();
        document.put("user", comEstablishment.getUser());
        document.put("password", comEstablishment.getPassword());
        document.put("nameEstablishment", comEstablishment.getNameEstablishment());
        document.put("NIT", comEstablishment.getNit());
        document.put("phoneNumber", comEstablishment.getPhoneNumber());
        document.put("CEOFirstName", comEstablishment.getCEOFirstName());
        document.put("CEOSecondName", comEstablishment.getCEOSecondName());
        document.put("CEOId", comEstablishment.getCEOId());
        document.put("email", comEstablishment.getEmail());
        //validations of unique fields must be inserted here!
        if (dbc.findOne(document) == null) {
            dbc.insert(document);
        } else {
            throw new PreexistingEntityException("The commertial establishment with name "
                    + comEstablishment.getNameEstablishment() + "couldn't be created. Already exist.");
        }
    }

    public void edit(ComEstablishment oldCommertial, ComEstablishment newCommertial) throws NonexistentEntityException, Exception {
        DBCollection dbc = null;
        BasicDBObject old = new BasicDBObject();
        BasicDBObject newco = new BasicDBObject();

        try {
            dbc = getDBCollection();

            old.put("user", oldCommertial.getUser());
            old.put("password", oldCommertial.getPassword());
            old.put("nameEstablishment", oldCommertial.getNameEstablishment());
            old.put("NIT", oldCommertial.getNit());
            old.put("phoneNumber", oldCommertial.getPhoneNumber());
            old.put("CEOFirstName", oldCommertial.getCEOFirstName());
            old.put("CEOSecondName", oldCommertial.getCEOSecondName());
            old.put("CEOId", oldCommertial.getCEOId());
            old.put("email", oldCommertial.getEmail());


            newco.put("user", newCommertial.getUser());
            newco.put("password", newCommertial.getPassword());
            newco.put("nameEstablishment", newCommertial.getNameEstablishment());
            newco.put("NIT", newCommertial.getNit());
            newco.put("phoneNumber", newCommertial.getPhoneNumber());
            newco.put("CEOFirstName", newCommertial.getCEOFirstName());
            newco.put("CEOSecondName", newCommertial.getCEOSecondName());
            newco.put("CEOId", newCommertial.getCEOId());
            newco.put("email", newCommertial.getEmail());
            dbc.update(dbc.findOne(old), newco);
        } catch (Exception ex) {
            if (ex.getLocalizedMessage().equals("can't save a null object")) {
                throw new NonexistentEntityException("The commertial establishment with name"
                        + oldCommertial.getNameEstablishment() + "couldn't be updated. May no longer exist");
            }
            throw ex;
        }

    }

    public void destroy(ComEstablishment comEstablishment) throws NonexistentEntityException {
        DBCollection dbc = null;
        BasicDBObject todestroy = new BasicDBObject();
        try {
            dbc = getDBCollection();
            todestroy.put("user", comEstablishment.getUser());
            todestroy.put("password", comEstablishment.getPassword());
            todestroy.put("nameEstablishment", comEstablishment.getNameEstablishment());
            todestroy.put("NIT", comEstablishment.getNit());
            todestroy.put("phoneNumber", comEstablishment.getPhoneNumber());
            todestroy.put("CEOFirstName", comEstablishment.getCEOFirstName());
            todestroy.put("CEOSecondName", comEstablishment.getCEOSecondName());
            todestroy.put("CEOId", comEstablishment.getCEOId());
            todestroy.put("email", comEstablishment.getEmail());
            dbc.remove(dbc.findOne(todestroy));
        } catch (Exception enfe) {
            throw new NonexistentEntityException("The commertial establishment with name "
                    + comEstablishment.getNameEstablishment() + "couldn't be deleted. May no longer exist.");
        }
    }

    public void findAdministratorEntities() {
        DBCollection dbc = getDBCollection();
        DBCursor cursor = dbc.find();

        // loop over the cursor and display the retrieved result
        while (cursor.hasNext()) {
            System.out.println(cursor.next());
        }


    }
}
